﻿using System;
using System.Collections.Generic;
using System.Text;
using Stone.Components;
using Dare.Utilities.Data;
using System.Data.Common;

namespace Stone.Data
{
    public abstract class EntityRelationDataProvider<Entity, EntityKey>
        where Entity : class, IEntity, IEntity<EntityKey>
        where EntityKey : struct
    {
        public DataProviderTemplate<Entity, EntityKey> OwnerProvider { get; private set; }
        public EntityRelationDataProvider(DataProviderTemplate<Entity, EntityKey> ownerProvider)
        {
            OwnerProvider = ownerProvider;
        }

        public abstract void Insert(TransactionManager mgr, DbCommand cmd, IEnumerable<Entity> entities);

        public virtual void Update(TransactionManager mgr, DbCommand cmd, IEnumerable<Entity> entities)
        {
            Delete(mgr, cmd, entities);
            Insert(mgr, cmd, entities);
        }

        public virtual void Delete(TransactionManager mgr, DbCommand cmd, IEnumerable<Entity> entities)
        {
            List<EntityKey> keys = new List<EntityKey>();
            foreach (Entity entity in entities)
            {
                keys.Add(entity.Id);
            }
            Delete(mgr, cmd, keys);
        }

        public abstract void Delete(TransactionManager mgr, DbCommand cmd, IEnumerable<EntityKey> keys);

        public abstract void Delete(TransactionManager mgr, DbCommand cmd, string whereClause);

        public abstract void Clean(DbCommand cmd);

        public abstract void Retrieve(TransactionManager mgr, DbCommand cmd, IEnumerable<Entity> entities);
    }
}
